import { Controller, Post, Body, Get, Req, UseInterceptors, Param } from '@nestjs/common';
import { AuthService } from './auth.service';
import { RegisterDto } from './dto/registerDto';
import { LoginDto } from './dto/loginDto';
import { UseGuards, SetMetadata } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { commonInterceptor } from 'src/interceptor/common.interceptor';
import { RoledGuard } from 'src/guard/role.guard';
import { RoleDto } from './dto/roleDto';
import { SetRoleDto } from './dto/setRoleDto';
@Controller('auth')
@UseInterceptors(commonInterceptor)
export class AuthController {
  constructor(private readonly authService: AuthService) {}
  //注册
  @Post('register')
  create(@Body() registerDto: RegisterDto) {
    return this.authService.register(registerDto);
  }
  //登录
  @Post('login')
  @SetMetadata('name', 'ajax')
  login(@Body() loginDto: LoginDto) {
    return this.authService.login(loginDto);
  }
  //获取全部user
  @Get('all')
  // @SetMetadata('roles', ['alex'])
  // @UseGuards(AuthGuard('jwt'), RoledGuard)
  // allUser(@Req() req) {
  //   console.log(req.user);
  //   return this.authService.allUser();
  // }
  allUser() {
    return this.authService.allUser();
  }
  //获取用户信息
  @Get('/userInfo')
  @UseGuards(AuthGuard('jwt'))
  getUserInfo(@Req() req) {
    return req.user;
  }
  //创建role
  @Post('role')
  createRole(@Body() role: RoleDto) {
    return this.authService.createRole(role);
  }
  //给用户分配role
  @Post(':id/roles')
  setRoleToUser(@Param('id') id: number, @Body() role: SetRoleDto) {
    return this.authService.setRoleToUser(id, role);
  }
}
